package com.my.selectcoursesystem.service.serviceImpl;

import com.my.selectcoursesystem.bean.Course;
import com.my.selectcoursesystem.dao.CourseDao;
import com.my.selectcoursesystem.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("CourseService")
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseDao courseDao;

    public CourseServiceImpl(CourseDao courseDao) {
        this.courseDao = courseDao;
    }

    public CourseServiceImpl() {
    }

    @Override
    public List<Course> getAllCourse() {
        return this.courseDao.findAll();
    }

    @Override
    public Course getCourseById(String cid) {
        return this.courseDao.getById(cid);
    }

    @Override
    public List<Course> searchByCourseName(String c_name) {
        return this.courseDao.findAllByCnameContains(c_name);
    }

    @Override
    public List<Course> searchByCid(String c_id) {
        return this.courseDao.findAllByCidContains(c_id);
    }

    @Override
    public List<Course> searchByCollege(String college) {
        return this.courseDao.findAllByCollegeContains(college);
    }

    @Override
    public List<Course> searchByType(String type) {
        return this.courseDao.findAllByTypeContains(type);
    }

    @Override
    public Course insert(Course course) {
        return this.courseDao.save(course);
    }

    @Override
    public List<Course> getCourseByTid(String tid) {
        return this.courseDao.findAllByTeacherid(tid);
    }

    @Override
    public Course getCourseByCidAndTid(String cid, String tid) {
        return this.courseDao.findByCidAndTeacherid(cid, tid);
    }

    @Override
    public Boolean deleteByCidAndTid(String cid, String tid) {
        try {
            this.courseDao.deleteByCidAndTeacherid(cid, tid);
            return true;
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
